package com.rw.coco.handler;

import com.rw.coco.api.bot.handler.GroupKickHandler;
import com.rw.coco.api.data.CocoDataApi;
import com.rw.coco.api.data.model.AccountModel;
import com.rw.coco.api.event.GroupKickEvent;
import com.rw.coco.api.log.CocoLogger;
import com.rw.coco.api.modle.Account;
import com.rw.coco.api.modle.Bot;
import com.rw.coco.api.modle.Group;
import com.rw.coco.service.AccountService;
import com.rw.coco.util.CocoData;
import com.rw.tool.util.string.StringUtil;

/**
 * 群踢人触发器
 */
public class CocoGroupKickHandler extends GroupIllegalHandler implements GroupKickHandler {
    private static final String KICK_MSG = "{}({}) 在群 {}({}) 中，被 {}({}) 踢出";

    public CocoGroupKickHandler(CocoDataApi dataApi, CocoLogger cocologger, CocoData cocoData) {
        super(dataApi, cocologger, cocoData);
    }

    @Override
    public void handle(GroupKickEvent groupKickEvent) {
        Account account = groupKickEvent.kickBy();
        // 如果踢的人不是自己，则不进行处理
        if (!StringUtil.equals(groupKickEvent.kicked().getId(), groupKickEvent.getBot().getId())) {
            return;
        }

        Group group = groupKickEvent.kickOutOf();
        Bot bot = groupKickEvent.getBot();

        // 查找用户
        AccountService accountService = new AccountService(dataApi.getPersistenceMapper());
        AccountModel accountModel = accountService.getOrDefaultAccount(account);

        // 封禁群聊，并输出日志
        banGroupAndAccount(KICK_MSG, account, group, bot, accountModel);

        // 如果操作者是管理员，则不进行删除操作
        if (accountModel.getSuperAdmin()) {
            return;
        }

        // 删除踢人者好友
        groupKickEvent.deleteKicker();
    }
}
